import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
    {
        path: '/',
        component: () => import("@/views/MainView.vue"),
        children: [
            {
                path: '/config',
                component: () => import("@/views/menu/ConfigView.vue"),
            },
            {
                path: '/area',
                component: () => import("@/views/menu/AreaView.vue"),
            },
            {
                path: '/cabinet',
                component: () => import("@/views/menu/CabinetView.vue"),
            },
            {
                path: '/books',
                component: () => import("@/views/menu/BooksView.vue"),
            },
            {
                path: '/reader',
                component: () => import("@/views/menu/ReaderView.vue"),
            },
            {
                path: '/equipLog',
                component: () => import("@/views/menu/EquipLogView.vue"),
            },
            {
                path: '/sysLog',
                component: () => import("@/views/menu/SysLogView.vue"),
            },
            {
                path: '/borrowLog',
                component: () => import("@/views/menu/BorrowLogView.vue"),
            },
            {
                path: '/user',
                component: () => import("@/views/menu/UserView.vue"),
            },
            {
                path: '/borrow',
                component: () => import("@/views/menu/BorrowAllView.vue"),
            },
            {
                path: '/configData',
                component: () => import("@/views/menu/ConfigDataView.vue"),
            },
            {
                path: '/configCenter',
                component: () => import("@/views/menu/configCenterView.vue"),
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import("@/views/LoginView.vue"),
    },
]
const router = createRouter({
    history: createWebHashHistory(),
    routes,
    scrollBehavior() {
        return {
            el: '#app',
            top: 0,
            behavior: 'smooth',
        }
    }
})
const whiteList = ['/login']
router.beforeEach(async (to, from, next) => {
    //获取token
    const hasToken = localStorage.getItem('Authorization')

    if (hasToken) {
        //token存在，如果当前跳转的路由是登录界面
        if (to.path === '/login') {
            next('/')
            NProgress.done()
        } else {
            next()
            NProgress.done()
        }
    } else {
        if (whiteList.indexOf(to.path) > -1) {
            next()
            NProgress.done()
        } else {
            next('/login')
            NProgress.done()
        }

    }
})

export default router
